<?php
class NodeController extends Ext_Controller_Action
{
	public function indexAction()
	{
		$node = new Cms_Models_Node();
		$this->view->contentToFront = $node->getNodesPageFront( 'magazine' );
	}
	
	public function searchAction()
	{
		$node = new Cms_Models_Content();
		require_once( APPLICATION_PATH . "/default/forms/Search.php" );
		$this->searchForm = new Default_Form_Search();
		
		$this->view->key = '';
		
		if( $this->_hasParam( 'key' ) ) {
			$this->view->key = $this->_getParam( 'key' );
			$types = array(1);
			$adapter = $node->findContent( $this->_getParam( 'key' ), $types, false );
			$this->view->paginator = new Ext_Paginator( $adapter, $this->_page );
		} 
	}	

	public function magazineAction()
	{
		if( !$this->_hasParam( 'nid' ) ) {
			$this->addError( "Debe seleccionar una revista" );
			return $this->_redirect( '/' );
		} 
		$this->view->headMeta()->appendName('keywords', 'importador de revistas, distribucion de revistas, revistas, importador, distribucon');
		$node = new Cms_Models_Content();
		$this->magazineId = $this->_getParam( 'nid' );
		$this->view->node = $node->find( $this->_getParam( 'nid' ) );
	}
	
	public function contactAction()
	{
		require_once( APPLICATION_PATH . "/default/forms/Contact.php" );
		$form = new Default_Form_Contact();
		if( $this->getRequest()->isPost() ) {
			if( $form->isValid( $this->getRequest()->getParams() )) {
				$body = $form->getValue('descripcion');
				$body .=  " \n ";
				$body .= "\n Nombre de contacto: " . $form->getValue('name') ."\n";
				$body .= "\n Telefono de contacto: " . $form->getValue('telefono') ."\n";
				$mail = new Zend_Mail();
				$mail->setBodyText( $body );
				$mail->setFrom($form->getValue('mail'), "Remitente");
				$mail->addTo("pablofmorales@gmail.com", "Destinatario");
				$mail->setSubject("Consultas desde la pagina njssrl.com.ar");
				$mail->send();
				$this->addMessage( 'Datos enviados con exito. En la brevedad le responderemos' );
			}
		}
		
		$this->view->form = $form;
	}
	
	public function viewAction()
	{
		$content = new Cms_Models_Content();
		if(! $this->_hasParam( 'nid' ) ) {
			$this->addError( "No se encontro el contenido" );
			return $this->_redirect( "/" );
		}
		$this->view->node = $content->find( $this->_getParam( 'nid' ) );	
	}
}